package com.store.app.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.store.app.BaseActivity;
import com.store.app.ExitApplication;
import com.store.app.MainActivity;
import com.store.app.R;
import com.store.app.imlife.utils.ToastTools;
import com.store.app.net.NetRequest;
import com.store.app.net.ifc.IOnHttpListener;
import com.store.app.utils.IDUtil;
import com.store.app.utils.SettingData;

import org.json.JSONObject;

/**
 * Created by Administrator on 2016/11/2.
 * 验证码收款页面
 */
public class CodePaymentActivity extends BaseActivity implements IOnHttpListener {
    private ImageView retreat;
    private TextView tv_send_code;
    private TextView tv_cancel;
    private TextView tv_ok;
    private NetRequest netRequest;
    private String phone;
    private CountDownTimer downtimer;//倒计时
    private EditText et_code;
    private String money;//支付金额
    private TextView tv_money;
    private String out_trade_no;
    private String consumer_id;

    private String price_original;
    private String discount_price;
    private String dikouData;
    private String rebateData;
    private String rewardData;
    private String transaction_no;
    private TextView tv_phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        setContentView(R.layout.activity_code_payment);

        phone = getIntent().getStringExtra("phone");

        money = getIntent().getStringExtra("money");
        consumer_id = getIntent().getStringExtra("consumer_id");
        price_original = getIntent().getStringExtra("price_original");
        discount_price = getIntent().getStringExtra("discount_price");
        dikouData = getIntent().getStringExtra("dikouData");
        rebateData = getIntent().getStringExtra("rebateData");
        rewardData = getIntent().getStringExtra("rewardData");
        initView();
        ExitApplication.getInstance().addActivity(this);
        //发送验证码
        netRequest = new NetRequest(this);
        //获取短信验证码验证信息
        netRequest.requestSendSmsCodeMess(5,MainActivity.app_token);
    }

    @Override
    public void onAfinalFail() {
        tv_send_code.setEnabled(true);
        ToastTools.showShort(CodePaymentActivity.this, "请检查本地网络");
//        if(downtimer!=null){
//            downtimer.cancel();
//            downtimer.onFinish();
//        }

    }

    @Override
    public void finish() {
        super.finish();
        if (downtimer != null) {
            downtimer.cancel();
            downtimer.onFinish();
        }
    }

    @Override
    public void onExecuteSuccess(int requestId, String result, String msg) {
        if (requestId == 1) {

        }
        //验证短信成功
        else if (requestId == 2) {
            //验证码收款
            out_trade_no = IDUtil.getOutTradeNo();
            netRequest.requestCodePay(3, money, out_trade_no, consumer_id);
        } else if (requestId == 3) {
            try {
                JSONObject jsonObject = new JSONObject(result);
                transaction_no = jsonObject.getString("transaction_no");
                netRequest.requestStoreCashierPromotion(4, out_trade_no,
                        consumer_id,
                        MainActivity.member_id,
                        "验证码支付", SettingData.PAYMENT_WAY_05,
                        price_original,
                        discount_price,
                        dikouData,
                        rebateData
                        , rewardData,
                        money,
                        transaction_no, "", "");
            } catch (Exception e) {
            }
        } else if (requestId == 4) {
            Log.v("zyl", "调促销规则接口成功");
            Intent intent = new Intent();
            intent.putExtra("resultMoney", money);
            intent.putExtra("transaction_no", transaction_no);
            intent.putExtra("out_trade_no", out_trade_no);
            setResult(Activity.RESULT_OK, intent);
            finish();
        }else if (requestId == 5) {
            //获取短信验证码验证信息
            downtimer.start();
            netRequest.requestSendSmsCode(1, MainActivity.app_token, phone, result);
        }
    }

    @Override
    public void onExecuteFail(int requestId, String msg) {
        ToastTools.showShort(CodePaymentActivity.this, msg);
        //获取验证码失败
        if (requestId == 1||requestId == 5) {
            tv_send_code.setEnabled(true);
            downtimer.cancel();
            downtimer.onFinish();
        }
    }

    private void initView() {

        tv_send_code = (TextView) findViewById(R.id.tv_send_code);
        tv_send_code.setEnabled(false);
        retreat = (ImageView) findViewById(R.id.retreat);
        retreat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tv_cancel = (TextView) findViewById(R.id.tv_cancel);
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tv_ok = (TextView) findViewById(R.id.tv_ok);
        tv_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(et_code.getText().toString().trim())) {
                    ToastTools.showShort(CodePaymentActivity.this, "请输入验证码");
                    return;
                }
                netRequest.requestValidSMS(2, phone, et_code.getText().toString().trim());
            }
        });

        downtimer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                tv_send_code.setText((millisUntilFinished / 1000) + " s");
            }

            @Override
            public void onFinish() {
                tv_send_code.setEnabled(true);
                tv_send_code.setText("重获验证码");
            }
        };

        tv_send_code.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(phone)) {
                    tv_send_code.setEnabled(false);
                    //获取短信验证码验证信息
                    netRequest.requestSendSmsCodeMess(5,MainActivity.app_token);

                }
            }
        });
        et_code = (EditText) findViewById(R.id.et_code);
        tv_money = (TextView) findViewById(R.id.tv_money);
        tv_money.setText(money + "");
        tv_phone = (TextView) findViewById(R.id.tv_phone);
        tv_phone.setText(phone);
    }

}
